package com.qf.hotel.controller.food;

import com.alibaba.fastjson.JSON;
import com.qf.hotel.constant.Constant;
import com.qf.hotel.controller.BaseServlet;
import com.qf.hotel.factory.BeanFactory;
import com.qf.hotel.pojo.Food;
import com.qf.hotel.pojo.FoodType;
import com.qf.hotel.pojo.ResultVO;
import com.qf.hotel.service.FoodService;
import com.qf.hotel.service.FoodTypeService;
import com.qf.hotel.utils.FileUploadUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@MultipartConfig
@WebServlet("/food")
public class FoodController extends BaseServlet {
    private FoodService foodService = (FoodService) BeanFactory.getBean(Constant.FOOD_SERVICE);
    private FoodTypeService foodTypeService = (FoodTypeService) BeanFactory.getBean("foodTypeService");
    /**
     * 查询菜品
     * @param request
     * @param response
     * @return
     */
    public String search(HttpServletRequest request, HttpServletResponse response){
        // 收参
        String keyword = request.getParameter("keyword");

        try {
            // 创建Food对象
            Food food = new Food();
            food.setFoodName(keyword);

            // 调用业务方法查询
            List<Food> foods = foodService.findByCondition(food);

            // 把结果存入request域
            request.setAttribute("foods",foods);

            // 可以在此处转发，也可以在return中写出转发的目标
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Constant.FORWARD  + ":/backend/detail/food/food-list.jsp";
    }

    /**
     * 接收添加food的请求
     * 把food插入到数据库
     * 插入数据之后跳转到food/search
     * @param request
     * @param response
     * @return
     */
    public String save(HttpServletRequest request, HttpServletResponse response){
        try {
            // 收参
            Map<String, String[]> parameterMap = request.getParameterMap();

            // 创建food对象并赋值
            Food food = new Food();
            BeanUtils.populate(food,parameterMap);

            // 上传文件，获取图片的名字，存入数据库
            String imageUrl = FileUploadUtils.uploadFile(request, "imageUrl", "/files/image");
            food.setFoodImage(imageUrl);

            // 调用业务方法执行上传
            int i = foodService.save(food);
            System.out.println("food===" + food);

            // 修改image的名字--在同一个文件夹中，文件名字不能重复，把image文件夹设置成惟一的

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Constant.REDIRECT + ":/food?method=search";
    }

    /**
     * 查找所有的菜系
     * @param request
     * @param response
     * @return
     */
    public String findFoodType(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        List<FoodType> foodTypes = foodTypeService.findByCondition(new FoodType());
        ResultVO  resultVO = new ResultVO();
        resultVO.setSuccess(true);
        resultVO.setData(foodTypes);
        return JSON.toJSONString(resultVO);
    }
}
